package deliver;

import java.io.IOException;
import java.sql.Connection;
import java.sql.Date;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;

import javax.servlet.RequestDispatcher;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import common.Number;
import common.Order;

import db.DBManager;

public class ReceiptServlet extends HttpServlet {

	@Override
	protected void doGet(HttpServletRequest req, HttpServletResponse resp)
			throws ServletException, IOException {
		// TODO 自動生成されたメソッド・スタブ
		perform(req, resp);
	}

	@Override
	protected void doPost(HttpServletRequest req, HttpServletResponse resp)
			throws ServletException, IOException {
		// TODO 自動生成されたメソッド・スタブ
		perform(req, resp);
	}
	private void perform(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {

		String[] order_num = req.getParameterValues("order_num");

		Connection con = null;
		Statement stm = null;
		Statement stm2 = null;
		ResultSet rs = null;
		ResultSet rs2 = null;
		

		try {
			con = DBManager.getConnection();
			stm = con.createStatement();
			stm2 = con.createStatement();

			//	注文番号の数だけ繰り返す
			for(int i=0 ; i<order_num.length ; i++){
				//	納品番号を取得(共通クラスNumberのメソッド)
				String receipt_num = Number.getReceipt();
				boolean ret=Order.setReceiptFlagByOrderNumber(order_num[i]);
				if(ret){
					//	納品請求情報を追加する
					String sql = "select * from order_info where num = " + order_num[i];
					rs = stm.executeQuery(sql);
					if (rs.next()) {
						//以下のinsert文でorderテーブルからデータを受け取りreceiptテーブルに書き込む
						String sql_ins = "insert into receipt (num,order_num,customer_code,date,cost_total,price_total,fee,deliver)"
								+ " values (?,?,?,?,?,?,?,?)";
						PreparedStatement ps = con.prepareStatement(sql_ins);
						ps.setString(1, receipt_num);											//納品書番号
						ps.setString(2, order_num[i]);										//注文番号
						ps.setString(3, rs.getString("customer_code"));				//顧客コード
						ps.setDate(4, new Date(System.currentTimeMillis()));		//納品書発行日
						ps.setInt(5, rs.getInt("cost_total"));								//合計原価金額
						ps.setInt(6, rs.getInt("price_total"));								//合計販売価格
						ps.setInt(7, rs.getInt("fee"));											//運賃
						ps.setDate(8, rs.getDate("deliver"));								//配達予定日
						ps.executeUpdate();

						String sql_ins2 ="insert into receipt_item (receipt_num,num,item_code,cost,price,orders)"
								+ " values (?,?,?,?,?,?)";
						ps =con.prepareStatement(sql_ins2);

						String sql2 = "select * from order_item where order_num = " + order_num[i];
						rs2 = stm2.executeQuery(sql2);
						//	受注商品の数だけ繰り返す
						while(rs2.next()){
							//	納品請求明細を追加する
							ps.setString(1, receipt_num);
							ps.setInt(2, rs2.getInt("num"));
							ps.setString(3, rs2.getString("item_code"));
							ps.setInt(4, rs2.getInt("cost"));
							ps.setInt(5, rs2.getInt("price"));
							ps.setInt(6, rs2.getInt("orders"));
							ps.executeUpdate();
						}
						rs2.close();
						ps.close();
					}
				}
			}
			rs.close();
		}
		catch (SQLException e) {
			rs = null;
			e.printStackTrace();
		}
		finally {
			if (stm != null) {
				try {stm.close();}
				catch (SQLException e) {e.printStackTrace();}
			}
			if (con != null) {
				try {con.close();}
				catch (SQLException e) {e.printStackTrace();}
			}
		}
		

		RequestDispatcher dispatcher = req.getRequestDispatcher("/operator/receipt.jsp");
		dispatcher.forward(req, resp);
	}
}
